package com.ht.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ht.shortlink.project.dao.entity.LinkNetworkStatsDO;
import com.ht.shortlink.project.dto.req.ShortLinkStatsGroupReqDTO;
import com.ht.shortlink.project.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;

public interface LinkNetworkStatsMapper extends BaseMapper<LinkNetworkStatsDO> {
    /**
     *  添加或更新网络监控数据
     */
    @Insert("INSERT INTO t_link_network_stats(gid, full_short_url, date, cnt, network, create_time, update_time, del_flag) " +
            "VALUES (#{linkNetworkStatsDO.gid}, #{linkNetworkStatsDO.fullShortUrl}, #{linkNetworkStatsDO.date}, #{linkNetworkStatsDO.cnt}, #{linkNetworkStatsDO.network}, NOW(), NOW(), 0) " +
            "ON DUPLICATE KEY " +
            "UPDATE cnt = cnt +  #{linkNetworkStatsDO.cnt};")
    void shortLinkNetworkStats(@Param("linkNetworkStatsDO") LinkNetworkStatsDO linkNetworkStatsDO);

    /**
     *  根据短链接查询访问网络监控数据
     */
    @Select("SELECT network,SUM(cnt) AS cnt FROM t_link_network_stats " +
            "WHERE full_short_url=#{param.fullShortUrl} AND gid=#{param.gid} AND date BETWEEN #{param.startDate} AND #{param.endDate} " +
            "GROUP BY full_short_url,gid,network;")
    List<HashMap<String, Object>> listStatsNetworkByLink(@Param("param") ShortLinkStatsReqDTO requestParam);

    /**
     *  根据分组查询访问网络监控数据
     */
    @Select("SELECT network,SUM(cnt) AS cnt FROM t_link_network_stats " +
            "WHERE gid=#{param.gid} AND date BETWEEN #{param.startDate} AND #{param.endDate} " +
            "GROUP BY gid,network;")
    List<HashMap<String, Object>> listStatsNetworkByGroup(@Param("param") ShortLinkStatsGroupReqDTO requestParam);
}
